/**
 * 群发相关
 * @authors 杨兴洲（of2502）
 * @date    2017/3/20 16:08
 * @version 1.0
 */

import {Router} from "express";
import UserService from "../../service/user-service";
import WxMessageService from "../../service/weixin-message-service";
import MassSendService from "../../service/massSend-service";
import logger from "../../util/logger";

const router = Router();

/**
 * 获取已群发消息列表
 */
router.post('/list', (req, res, next) => {
    (async () => {
        try {
            const {userID} = res.locals;
            const access_token = await UserService.getAccessToken(userID);
            const result = await WxMessageService.getAutoReplyRules({access_token});
            logger.debug(result);
            res.status(200).json(result);
        } catch (error) {
            next(error);
        }
    })();
});

/**
 * 群发消息列表(根据openid)
 */
router.post('/send', (req, res, next) => {
    (async () => {
        try {
            const {userID} = res.locals;
            let {send_ignore_reprint, touser, message} = req.body;
            let access_token = await UserService.getAccessToken(userID);
            let result = await WxMessageService.massSendByID({
                access_token,
                send_ignore_reprint,
                touser,
                message
            });
            if (result.errcode === 0) { // 存储msgid到数据库
                message.msgID = result.msg_id;
                await MassSendService.create({userID, message});
            }
            logger.debug(result);
            res.status(200).json(result);
        } catch (error) {
            next(error);
        }
    })();
});

/**
 * 群发消息列表(根据标签)
 */
router.post('/sendall', (req, res, next) => {
    (async () => {
        try {
            let {send_ignore_reprint, is_to_all, tag_id, message} = req.body;
            const {userID} = res.locals;
            let access_token = await UserService.getAccessToken(userID);
            let result = await WxMessageService.massSendByTag({
                access_token,
                send_ignore_reprint,
                is_to_all,
                tag_id,
                message
            });
            if (result.errcode === 0) { // 存储msgid到数据库
                message.msgID = result.msg_id;
                await MassSendService.create({userID, message});
            }
            logger.debug(result);
            res.status(200).json(result);
        } catch (error) {
            next(error);
        }
    })();
});


/**
 * 群发预览
 */
router.post('/preview', (req, res, next) => {
    (async () => {
        try {
            let {touser, message} = req.body;
            let {userID} = res.locals;
            let access_token = await UserService.getAccessToken(userID);
            let result = await WxMessageService.massSendPreview({access_token, touser, ...message});
            logger.debug(result);
            res.status(200).json(result);
        } catch (error) {
            next(error);
        }
    })();
});


/**
 * 群发删除
 */
router.post('/delete', (req, res, next) => {
    (async () => {
        try {
            let {msg_id} = req.body;
            let {userID} = res.locals;
            let access_token = await UserService.getAccessToken(userID);
            let result = await WxMessageService.massSendDelete({access_token, msg_id});
            logger.debug(result);
            res.status(200).json(result);
        } catch (error) {
            next(error);
        }
    })();
});

/**
 * 查询群发消息状态
 */
router.post('/status', (req, res, next) => {
    (async () => {
        try {
            let {msg_id} = req.body;
            let {userID} = res.locals;
            let access_token = await UserService.getAccessToken(userID);
            let result = await WxMessageService.massSendStatus({access_token, msg_id});
            logger.debug(result);
            res.status(200).json(result);
        } catch (error) {
            next(error);
        }
    })();
});

export default router;
